<?php

/**
 * @author Alex McArrow
 * @copyright 2008
 */
 
function uniprint($compare,$return,$formout)
{
	global $BIGDATAARRAY;
	global $ARRAYS;
	global $DEBUGDATA;
	global $KEYS;


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
$DEBUGDATA[]="Uniprint(compare,return,formout)";
$DEBUGDATA[]="compare: ".$compare;
$DEBUGDATA[]="return: ".$return;
$DEBUGDATA[]="formout: ".$formout;
$DEBUGDATA[]="-------------------------------------------------------";
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//

$returnin=$BIGDATAARRAY[$_REQUEST['usethis']]['returntype'];

$t1=explode("|",$returnin);
$t2=explode(",",$t1[$_REQUEST['typeform']]);
$returnin=explode(";",$t2[2]);
$returninarray=array();


foreach ($returnin as $key=>$value)
{
///// foreach

$pos1 = strpos($value, "[");
while ($pos1 !== false)
{
$pos1 = strpos($value, "[");	
if ($pos1 !== false)
{
    $pos2 = strpos($value, "]", $pos1);
    $pat = substr($value, $pos1, $pos2 - $pos1 + 1);
    $da = substr($value, $pos1 + 1, $pos2 - $pos1 - 1);  
	
	$out="";
	
	$value = str_replace($pat,$out,$value);
	$returnin[$key] = str_replace($pat,$out,$value);
	$returninarray[$value] = $da;
	
}
$pos1 = strpos($value, "[");
}

///// foreach	
}


$return=explode(";",$return);

$BODY = $formout;


///// [in(id)]
$pos1 = strpos($BODY, "[in(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[in(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 4, $pos2 - $pos1 - 4);  
	
	$daa=explode(";",$da);
	
	$out="";
	
	foreach ($ARRAYS[$daa[1]] as $key=>$value)
	{	

		if ($value[0] == $KEYS[$daa[0]])
		{
			$out=$value[1];
		}
	}
		
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[in(");
}
///// [in(id)]

///// [count(id)]
$pos1 = strpos($BODY, "[count(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[count(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 7, $pos2 - $pos1 - 7);     
	$out = 0;		
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{
		if ($value[$tableid] != "")
		{
			$out++;
		}
	}   
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[count(");
}
///// [count(id)]

///// [sum(id)]
$pos1 = strpos($BODY, "[sum(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[sum(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 5, $pos2 - $pos1 - 5);     
	$summ = 0;	
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{
		if ($value[$tableid] != "")
		{
			$summ=$summ+$value[$tableid];
		}
	}   
	
	$out = $summ;
	
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[sum(");
}
///// [sum(id)]

///// [mean(id)]
$pos1 = strpos($BODY, "[mean(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[mean(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 6, $pos2 - $pos1 - 6);     
	$count = 0;	
	$summ = 0;	
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{
		if ($value[$tableid] != "")
		{
			$count++;
			$summ=$summ+$value[$tableid];
		}
	}   
	
	$out = round($summ/$count,2);
	
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[mean(");
}
///// [mean(id)]

///// [max(id)]
$pos1 = strpos($BODY, "[max(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[max(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 5, $pos2 - $pos1 - 5);     
	$max = -100000000000000;	
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{
		if ($value[$tableid] != "")
		{	
			if ($value[$tableid] > $max)
			{
				$max=$value[$tableid];
			}
		}
	}   
	
	$out = $max;
	
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[max(");
}
///// [max(id)]

///// [min(id)]
$pos1 = strpos($BODY, "[min(");
while ($pos1 !== false)
{
$pos1 = strpos($BODY, "[min(");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, ")]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 2);
    $da = substr($BODY, $pos1 + 5, $pos2 - $pos1 - 5);     
	$max = 100000000000000;	
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{
		if ($value[$tableid] != "")
		{	
			if ($value[$tableid] < $max)
			{
				$max=$value[$tableid];
			}
		}
	}   
	
	$out = $max;
	
    $BODY = str_replace($pat,$out,$BODY);
}
$pos1 = strpos($BODY, "[min(");
}
///// [min(id)]

///// [list]
$pos1 = strpos($BODY, "[list");	
if ($pos1 !== false)
{
    $pos2 = strpos($BODY, "]", $pos1);
    $pat = substr($BODY, $pos1, $pos2 - $pos1 + 1);
    $da = substr($BODY, $pos1 + 5, $pos2 - $pos1 - 5);   
	
	
	if (count($returnin) > 1)
	{
	  
	$out = "<table border='0' id='list'>";
	$count = 0;
	$out.="<tr id='title'>";
	if ($t2[3]=="num")
	{
	$out.="<td align='center'>#</td>";	
	}
	
	$daa = $returnin;
	
	foreach ($daa as $key=>$value)
	{
		$out.="<td align='center'>&nbsp;".$value."&nbsp;</td>";
	}
	$out.="</tr>";
		
	$tableid=array_search($da,$return);	
	foreach ($compare as $key=>$value)
	{	
		$out.="<tr>";
		$count++;
		if ($t2[3]=="num")
		{
		$out.="<td>".$count."</td>";
		}
		foreach($daa as $k=>$v)
		{	
			$tableid=array_search($v,$return);
						
			if ($returninarray[$v] == "")
			{			
				$out.="<td>&nbsp;".$value[$tableid]."&nbsp;</td>";
			}
			else
			{
				
				foreach ($ARRAYS[$returninarray[$v]] as $ak=>$av)
				{
					if ($av[0] == $value[$tableid])
					{
						$out.="<td>&nbsp;".$av[1]."&nbsp;</td>";
					}
					
				}
				
				
			}
		}
		$out.="</tr>";
	}   
	
	$out.="</table>";
	
	}
	else
	{
	$out="";	
	}
	
    $BODY = str_replace($pat,$out,$BODY);

$pos1 = strpos($BODY, "[list");
}
///// [list(id)]

return $BODY;	
	
}

?>